package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.TaskListAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.ExtraValueContent;
import com.isunland.managesystem.entity.Task;
import com.isunland.managesystem.entity.TaskList;
import java.util.ArrayList;
import java.util.HashMap;

public class FinishiListFragment extends BaseListFragment {
  private static final int REQUEST_QUERY = 0;
  private ArrayList<Task> mTasks;
  //初始化查询条件
  private String mtypeCode = "";//流程分类code
  private String mtypeName = "";//流程分类name
  private String mifApproveCode = "";//是否完成审批code
  private String mifApproveName = "";//是否完成审批name
  private String mapplyName = "";//申请人或部门名
  private String mtitle = "";//标题
  private String mBeginTime = "";//开始时间
  private String mEndTime = "";//结束时间

  // 菜单按钮，标题和返回
  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_query_refresh, menu);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case R.id.menu_item_refresh:
        refresh();
        return true;
      case R.id.menu_item_querys:
        //查询
        ExtraValueContent contet =
            new ExtraValueContent(mtypeName, mtypeCode, mifApproveName, mifApproveCode, mapplyName,
                mtitle, mBeginTime, mEndTime);
        Intent query = new Intent(getActivity(), TodoQueryActivity.class);
        query.putExtra(ToDoQueryFragment.EXTRA_CONTENT, contet);
        query.putExtra(ToDoQueryFragment.EXTRA_FROM, TaskListFragment.MY_COMPLETED);
        startActivityForResult(query, REQUEST_QUERY);
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override public String getUrl() {
    return ApiConst.URL_MY_COMPLETED;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();
    params.put("curPage", "1");
    params.put("pageSize", "1000");
    //        params.put("nodePath", mtypeCode);//流程分类code
    params.put("Q_subject_SUPL", mtitle);//标题
    params.put("Q_begincreatetime_DL", mBeginTime);//申请开始时间
    params.put("Q_endcreatetime_DG", mEndTime);//申请结束时间
    return params;
  }

  @Override public void resolveResponse(String arg0) {

    TaskList taskList = new Gson().fromJson(arg0, TaskList.class);
    ArrayList<Task> tasks = taskList.getDataList();
    if (mTasks == null) {
      mTasks = new ArrayList<>();
    }
    mTasks.clear();
    mTasks.addAll(tasks);
    setAdapter(mTasks);
  }

  private void setAdapter(ArrayList<Task> mTasks2) {
    // 将mtasks放入适配器显示
    TaskListAdapter adapter = new TaskListAdapter(getActivity(), mTasks2, 3);
    setListAdapter(adapter);
    ((TaskListAdapter) getListAdapter()).notifyDataSetChanged();
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    Task task = ((TaskListAdapter) getListAdapter()).getItem(position - 1);
    onTaskSelected(task);
  }

  public void onTaskSelected(Task task) {
    task.setmMenuId(Task.MY_COMPLETED);
    Intent i = new Intent(getActivity(), TaskPagerActivity.class);
    i.putExtra(TaskPagerActivity.EXTRA_TASK_CONTENT, task);
    startActivity(i);
  }

  public void refresh() {
    volleyPost();
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) return;

    if (requestCode == REQUEST_QUERY) {
      //查询
      ExtraValueContent content =
          (ExtraValueContent) data.getSerializableExtra(ToDoQueryFragment.EXTRA_CONTENT);
      //            mtypeName = content.getExtName1();
      //            mtypeCode = content.getExtCode1();
      mifApproveName = content.getExtName2();
      mifApproveCode = content.getExtCode2();
      mapplyName = content.getExtName3();
      mtitle = content.getExtName4();
      mBeginTime = content.getExtName5();
      mEndTime = content.getExtName6();

      refresh();
    }
  }
}
